﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNA_Game_Framework
{
	public class Button : Control
	{
		public delegate void RunMethod(object sender, EventArgs e);
		public event RunMethod Click;
		
		Texture2D buttonTexture;
		SpriteFont font;
		string text;

		internal Texture2D ButtonTexture
		{
			get { return buttonTexture; }
		}

		internal SpriteFont Font
		{
			get { return font; }
		}

		public override Vector2 Size
		{
			get { return new Vector2(buttonTexture.Width,buttonTexture.Height); }
		}

		public string Text
		{
			get { return text; }
			set { text = value; }
		}

		public Button(object parent, string name, Vector2 position, Theme.Button theme)
			: base(name, parent, position)
		{
			font = theme.font;
			buttonTexture = theme.buttonTexture;
			text = theme.text;
		}

		public Button(object parent, string name, Vector2 position, string text, Theme.Button theme)
			: base(name, parent, position)
		{
			font = theme.font;
			buttonTexture = theme.buttonTexture;
			this.text = text;
		}

		internal override void Draw(SpriteBatch spriteBatch)
		{
			Vector2 textSize = font.MeasureString(text);
			Vector2 textPosition = WindowPosition + new Vector2((buttonTexture.Width - textSize.X) / 2, (buttonTexture.Height - textSize.Y) / 2);

			spriteBatch.Draw(buttonTexture, WindowPosition, Color.White);
			spriteBatch.DrawString(font, text, textPosition, Color.Black);
		}

		override internal bool OnLeftClick(MouseEventArgs e)
		{
			if (Bounds.Contains(new Point((int)e.Position.X, (int)e.Position.Y)))
			{
				OnClick(EventArgs.Empty);
				return true;
			}
			return false;
		}

		protected virtual void OnClick(EventArgs e)
		{
			if (Click != null)
			{
				Click(this, e);
			}
		}
	}
}
